﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microhard.Permission.Interface;
using Micrhard.FMCG.Server.Models;
using Micrhard.FMCG.Server.Interface;
using Microsoft.Practices.Unity;
using Microhard.Permission.MVCFilter;
using Micrhard.FMCG.Server.Impl;
using FluentValidation.Mvc;
using Microhard.Core.DBUtility;
using System.Text;

namespace Microhard.FMCG.WebUI.Areas.WrokPlan.Controllers
{
    public class ApproveDailyPlanController : MicroHardBaseController<DailyModel>
    {
        private IDailyApprove _resp = null;
        [Dependency]
        public IDailyApprove Category
        {
            set
            {
                base._repository = value;
                _resp = value;
            }
            get
            {
                return _resp;
            }
        }

        [MVCFilterAtrForLogon(Order = 1)]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Approve", Order = 2)]//功能权限验证
        [MVCFilterAtrForCustomeDataPermission(typeof(DailyCommentDataPermissionValidator), DataScopeactionParameterName = "scope", Order = 3)]//数据权限验证
        public ActionResult Approve(Guid identity, Core.DBUtility.ScopeResult scope)
        {
            var model = _repository.GetModelByID(identity);
            BasePermissionModel bpm = model as BasePermissionModel;
            if (bpm != null)
            {
                bpm.OperationStatus = OperationStatus.Update;
            }
            return View(model);
        }


        [MVCFilterAtrForLogon(Order = 1)]//登录验证
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Approve", Order = 2)]//功能权限验证
        [MVCFilterAtrForCustomeDataPermission(typeof(DailyCommentDataPermissionValidator), DataScopeactionParameterName = "scope", Order = 3)]//数据权限验证
        public ActionResult SaveApprove([CustomizeValidator(RuleSet = "Approve")]DailyModel model, Core.DBUtility.ScopeResult scope)
        {
            if (ModelState.IsValid)
            {
                _repository.scope = scope;
                var rtn = _repository.Update(model);

                rtn.ExecutedModel = model;
                this.HttpContext.Items["executedResult"] = rtn;

                if (rtn.IsSucceed)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    StringBuilder errorMsg = new StringBuilder();
                    foreach (var err in rtn.ErrorMsg)
                    {
                        errorMsg.Append(err.Value);
                    }
                    TempData["executedResult"] = errorMsg.ToString();
                }
            }
            return View("Approve", model);
        }
    }
}
